package com.lanxin.xxform.services.impls;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanxin.xxform.aspects.exceptions.ServiceException;
import com.lanxin.xxform.beans.Answer;
import com.lanxin.xxform.beans.AnswerItem;
import com.lanxin.xxform.beans.Paper;
import com.lanxin.xxform.mappers.AnswerItemMapper;
import com.lanxin.xxform.mappers.AnswerMapper;
import com.lanxin.xxform.services.AnswerService;
import com.lanxin.xxform.services.PaperService;
import com.lanxin.xxform.tools.JetCriteria;
import com.lanxin.xxform.tools.JetDate;

@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private PaperService paperService;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerItemMapper itemMapper;

    @Override
    public long createAnswer(long pid, String name, String phone) {
        //获取问卷详情
        Paper paper = paperService.get(pid);
        if (paper.getStatus() != PaperService.STATUS_OPEN) {
            throw new ServiceException("抱歉，当前问卷调查已关闭提交功能");
        }
        //去重
        JetCriteria criteria = new JetCriteria();
        criteria.add("pid", pid);
        criteria.add("phone", phone);
        if (answerMapper.count(criteria) > 0) {
            throw new ServiceException("同一个用户只能提交一次问卷");
        }
        //保存
        Answer answer = new Answer();
        answer.setPid(pid);
        answer.setName(name);
        answer.setPhone(phone);
        answer.setCreateTime(JetDate.timeNow());
        answer.setStatus(STATUS_NEW);
        answer.setUid(paper.getUid());
        if(answerMapper.insert(answer) > 0) {
            return answer.getId();
        }
        return 0;
    }

    @Override
    public int saveAnswer(long aid, long sid, String title, String content) {
        AnswerItem item = new AnswerItem();
        item.setAid(aid);
        item.setSid(sid);
        item.setTitle(title);
        item.setContent(content.trim());
        return itemMapper.insert(item);
    }

    private JetCriteria searchCondition(long uid, long pid, int status, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.add("uid", uid);
        if (pid > 0)
            criteria.add("pid", pid);
        if (status > 0)
            criteria.add("status", status);
        if (! keyword.isEmpty()) {
            criteria.addCondition("(name like '%"+keyword+"%' OR phone like '%"+keyword+"%')", JetCriteria.OPT_AND);
        }
        return criteria;
    }

    @Override
    public int getCount(long uid, long pid, int status, String keyword) {
        return answerMapper.count(searchCondition(uid, pid, status, keyword));
    }

    @Override
    public List<Answer> getList(int offset, int limit, long uid, long pid, int status, String keyword) {
        JetCriteria criteria = searchCondition(uid, pid, status, keyword);
        criteria.setPage(offset, limit);
        criteria.orderBy("id", JetCriteria.ORDER_DESC);
        return answerMapper.findAll(criteria);
    }
    
}
